package com.greenantmall.adapter;

import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.greenantmall.R;

public class ChooseDateListAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	
	private List<ChooseDateBean> mDataList;
	
	public static class ChooseDateBean {
		public boolean isSelected = false;
		public Calendar calendar;
	}
	
	public ChooseDateListAdapter(Context context, List<ChooseDateBean> dataList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
		mDataList = dataList;
	}

	@Override
	public int getCount() {
		if(mDataList != null) {
			return mDataList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View child = convertView;
		ViewHolder holder = null;
		if(child == null) {
			child = mLayoutInflater.inflate(R.layout.choose_date_list_item, null);
			holder = new ViewHolder(child);
			child.setTag(holder);
		}
		else {
			holder = (ViewHolder)child.getTag();
		}
		
		ChooseDateBean bean = mDataList.get(position);
		holder.getTvDate().setText(DateFormat.format("HH:mm", bean.calendar));
		holder.getCheckBox().setChecked(bean.isSelected);
		
		return child;
	}
	
	private class ViewHolder {
		private View mContentView;
		private TextView mTvDate;
		private CheckBox mCheckBox;
		
		public ViewHolder(View contentView) {
			mContentView = contentView;
		}

		public TextView getTvDate() {
			if(mTvDate == null) {
				mTvDate = (TextView)mContentView.findViewById(R.id.choose_date_list_item_tv);
			}
			return mTvDate;
		}

		public CheckBox getCheckBox() {
			if(mCheckBox == null) {
				mCheckBox = (CheckBox)mContentView.findViewById(R.id.choose_date_list_item_cb);
			}
			return mCheckBox;
		}
	}
}








